package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Depot;
import com.sjlh.centurylianhua.entity.PurchaseLog;
import com.sjlh.centurylianhua.entity.PurchaseLogInfo;
import com.sjlh.centurylianhua.mapper.PurchaseLogMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Resource
    private PurchaseLogMapper purchaseMapper;
    @RequestMapping("/add")
    public Object add(@RequestBody PurchaseLog purchaseLog) {
        System.out.println(purchaseLog);
        purchaseLog.setCodeId("SJLH"+ new Date().getTime());
        System.out.println(purchaseLog);
        long r=purchaseMapper.insert(purchaseLog);
        long id=purchaseLog.getId();
        if (r==1)
            return new ResultData(true,20000,"添加成功",true,id);
        else
            return new ResultData(false,20000,"添加失败",false,null);

    }
    @RequestMapping("/del")
    public Object del(@RequestBody PurchaseLog purchaseLog) {
        System.out.println(purchaseLog);
        long r=purchaseMapper.deleteById(purchaseLog.getId());
        if (r==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20000,"删除失败",false,null);

    }

    /**
     * 根据地址和仓库名称分页查询
     *
     * */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon){
        System.out.println(searchCon);
        QueryWrapper<PurchaseLog> wrapper = new QueryWrapper<>();
        wrapper.eq(searchCon.getTypeName()!=null,"supplier_id",searchCon.getTypeName());
        wrapper.eq(searchCon.getTypeName2()!=null,"depot_id",searchCon.getTypeName2());
        wrapper.eq(searchCon.getTypeName3()!=null,"state",searchCon.getTypeName3());
        wrapper.ge(StringUtils.isnotNullObj(searchCon.getBeginDate()),"creat_time",searchCon.getBeginDate());
        wrapper.le(StringUtils.isnotNullObj(searchCon.getEndDate()),"creat_time",searchCon.getEndDate());
        //判断是否分页
        if(searchCon.getPage()==null){
            List<PurchaseLog> list = purchaseMapper.selectList(wrapper);
            long count = purchaseMapper.selectList(wrapper).size();
            ResultData rs=new ResultData(true,20000,"成功",list,count);
            return rs;
        }else {
            // 封装分页对象
            Page<PurchaseLog> page = new Page<PurchaseLog>(searchCon.getPage(), searchCon.getLimit());
            Page<PurchaseLog> depotPage = purchaseMapper.selectPage(page, wrapper);
            List<PurchaseLog> list=depotPage.getRecords();//所有数据
            long count=depotPage.getTotal();//总条数
            ResultData rs=new ResultData(true,20000,"成功",list,count);
            return rs;
        }
    }
    @RequestMapping("/set")
    public Object setRemark(@RequestBody PurchaseLog purchaseLog) {
        System.out.println(purchaseLog);
        UpdateWrapper<PurchaseLog> wrapper = new UpdateWrapper<>();
        wrapper.set("state", purchaseLog.getState());
        wrapper.eq("id",purchaseLog.getId());
        long r=purchaseMapper.update(purchaseLog,wrapper);
        if (r==1)
            return new ResultData(true,20000,"修改成功",true,null);
        else
            return new ResultData(false,20000,"修改失败",false,null);

    }
}
